# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=quantum-espresso
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.4
pkgrel=2
#_d3q_commit=de4718351e7bbb9d1d12aad2b7ca232d06775b83
_devxlib_commit=a6b89ef77b1ceda48e967921f1f5488d2df9226d
_fox_commit=3453648e6837658b747b895bb7bef4b1ed2eac40
_pw2qmcpack_commit=f72ab25fa4ea755c1b4b230ae8074b47d5509c70
#_qe_gipaw_commit=490046d300ec23444504bfad0562baa1c71f9345
pkgdesc="opEn-Source Package for Research in Electronic Structure, Simulation, and Optimization (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='http://www.quantum-espresso.org/'
msys2_repository_url="https://gitlab.com/QEF/q-e"
msys2_references=(
  'aur: quantum-espresso'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libmbd"
         #"${MINGW_PACKAGE_PREFIX}-libxc"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-wannier90"
             #"${MINGW_PACKAGE_PREFIX}-environ"
             "git")
source=("https://gitlab.com/QEF/q-e/-/archive/qe-${pkgver}/q-e-qe-${pkgver}.tar.bz2"
        #"d3q::git+https://github.com/anharmonic/d3q#commit=${_d3q_commit}"
        "devxlib::git+https://gitlab.com/max-centre/components/devicexlib#commit=${_devxlib_commit}"
        "fox::git+https://github.com/pietrodelugas/fox#commit=${_fox_commit}"
        "pw2qmcpack::git+https://github.com/QMCPACK/pw2qmcpack#commit=${_pw2qmcpack_commit}"
        #"qe-gipaw::git+https://github.com/dceresoli/qe-gipaw#commit=${_qe_gipaw_commit}"
        "001-use-srand-instead-of-srandom.patch"
        "002-fix-build-devxlib-on-mingw.patch"
        "003-fix-install-c-libraries.patch"
        "004-fix-executable-suffix-on-mingw.patch")
sha256sums=('e17e7090b42db4687c5f304f8a47c3e90a0b05b6e345cdc54727465b5f0b2e33'
            'ef02c13a1ea73f38f0b81edd281c991c25bf045307ceb305f5c0f96b743d1556'
            '8625181b53f2b616a0f704507b4f2381f7b9fabe02078dfb74ab41ee11f5c260'
            'f0f5ebece3f0142e0d0b476db9547d8501de80259bc6409e9b108b887e4cc058'
            '2e18a13c194328516176c2bc2429ac230c9fc585381aa238e586b6034a7df0cb'
            'faaf147678e0c79a6678c5463dd5a466769257968275ef562f40ad1a0c2aacc6'
            'c889430a51d7b910723728c40ca73f183a93db58b1d03b02c6d7c98aea481e8b'
            '918cd94d6c770c88f59836ab4aedde749ac5e78da850556ad1a7acbf99109537')
noextract=("q-e-qe-${pkgver}.tar.bz2")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  echo "Extracting q-e-qe-${pkgver}.tar.bz2..."
  tar -xjf q-e-qe-${pkgver}.tar.bz2 || true

  for _ext_prj in devxlib fox pw2qmcpack; do
    mv ${_ext_prj} q-e-qe-${pkgver}/external/
  done

  cd "${srcdir}"/q-e-qe-${pkgver}
  apply_patch_with_msg \
    001-use-srand-instead-of-srandom.patch \
    002-fix-build-devxlib-on-mingw.patch \
    003-fix-install-c-libraries.patch \
    004-fix-executable-suffix-on-mingw.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export FFLAGS=" -Wno-missing-include-dirs -w"
  fi

  CFLAGS+=" -Wno-incompatible-pointer-types" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DQE_ENABLE_FOX=ON \
      -DQE_ENABLE_HDF5=ON \
      -DQE_ENABLE_OPENMP=ON \
      -DQE_ENABLE_ENVIRON=OFF \
      -DQE_ENABLE_LIBXC=OFF \
      -DQE_ENABLE_MPI=OFF \
      -DQE_ENABLE_TEST=OFF \
      -DQE_ENABLE_PLUGINS="pw2qmcpack;legacy" \
      -DQE_DEVICEXLIB_INTERNAL=ON \
      -DQE_FOX_INTERNAL=ON \
      -DQE_MBD_INTERNAL=OFF \
      -DQE_WANNIER90_INTERNAL=OFF \
      -DQE_FFTW_VENDOR="FFTW3" \
      -DBLA_VENDOR=OpenBLAS \
      -S q-e-qe-${pkgver} \
      -B build-${MSYSTEM}

  "${MINGW_PREFIX}"/bin/cmake.exe --build build-${MSYSTEM}
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install build-${MSYSTEM}

  install -Dm644 "${srcdir}"/q-e-qe-${pkgver}/License \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/qe/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Rename cp to qe_cp, to not be used instead of /usr/bin/cp to copy files 
  mv "${pkgdir}${MINGW_PREFIX}"/bin/{cp,qe_cp}.exe
}
